<!--部门添加-->
<template>
<div style="background:#ffffff;">
  <div>
        <el-row style="background:#ffffff;">
        <el-col :span="20">
         
            <el-button type="info" @click="$router.go(-1)" size="mini"  icon="el-icon-arrow-left">返回</el-button>

          </el-col>
    </el-row>
    </div>
    <el-row>
      <el-col :span="24">
        <el-form :model="DepartmentEdit" :rules="rules" ref="Edit" label-width="100px" label-position="top" :inline="true" size="medium" >
          <template ></template>
          <el-form-item label="名称" prop="DepartmentName">
           <el-input placeholder v-model="DepartmentEdit.DepartmentName"></el-input>
          </el-form-item>
          <el-form-item label="电话" prop="Phone1">
           <el-input placeholder v-model.number="DepartmentEdit.Phone1"></el-input>
          </el-form-item>
          <el-form-item label="传真" prop="Fax">
            <el-input placeholder v-model="DepartmentEdit.Fax"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="Email">
            <el-input placeholder v-model="DepartmentEdit.Email"></el-input>
          </el-form-item>
          <el-form-item label="地址" prop="Address">
            <el-input placeholder v-model="DepartmentEdit.Address"></el-input>
          </el-form-item>
          <el-form-item label="排序" prop="LevelID">
            <el-input placeholder v-model.number="DepartmentEdit.LevelID"></el-input>
          </el-form-item>
          <el-form-item label="上下班时间" prop="Monday">
            <el-input placeholder v-model="DepartmentEdit.Monday"></el-input>
          </el-form-item>          
        </el-form>
      </el-col>
    </el-row>
    <div>
      <el-button type="primary" @click="GetDepaerment('Edit')" icon="el-icon-check" size="mini">保存</el-button>
      <el-button @click="$router.go(-1)" icon="el-icon-close" size="mini">取消</el-button>    
    </div>
    </div>
</template>
<style lang="scss" scoped>
.el-row {
  margin-bottom: 10px;
}
.el-form-item {
  width: 49%;
}
</style>
<script>
export default {
    data() {
      return {
        DepartmentEdit:{
          DepartmentName:"",
          Phone1:"",
          Fax:"",
          Email:"",
          Address:"",
          LevelID:"",
          Monday:""
        },
        Type:"Add",
        rules:{
          DepartmentName:[
          {required:true,message:'请输入内容',trigger:'blur'}
        ],
          Phone1:[
            {required:true,message:'请输入内容',trigger:'blur'},          
           //{min:11,max:11,message:"请输入正确的电话号码!",trigger:'blur'}
        ],
          Fax:[
          {required:true,message:'请输入内容',trigger:'blur'}
        ],
          Email:[
          {required:true,message:'请输入内容',trigger:'blur'}
        ],
          Address:[
          {required:true,message:'请输入内容',trigger:'blur'}
        ],
          LevelID:[
          {required: true, message: '请输入内容', trigger: 'blur'},
          {type:'integer',message:'请输入数字',trigger:'change'}
        ],
          Monday:[
          {required:true,message:'请输入内容',trigger:'blur'}
        ],
        }
      }
    },
    mounted(){
      if(Object.keys(this.$route.query).length){
        this.Type = "Edit";
        this.$axios({
        url: "/HrApi/Department/GetDepartmentInfo",
        method: "GET",
        params:{
          id: this.$route.query["id"]
        },
        }).then((res) => {
        if (!res.data.code) {
          this.DepartmentEdit = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      });
      }
    },
    methods:{
      GetDepaerment(DepartmentEdit){
        this.$refs[DepartmentEdit].validate((valid) => {
        if (!valid) {
          return false;
        }
        //执行请求
        this.$axios({
          url: "/HrApi/Department/UpdateDepartment",
          method: "POST",
          data: this.$Qs.stringify({
            pk_User:this.$store.state.UserInfo.pk_User,
            Department: JSON.stringify(this.DepartmentEdit),
            Type: this.Type
          }),
        }).then((res) => {
          if (!res.data.code) {
            //添加成功，重置表单
            if(this.Type==="Add"){
              this.$refs[DepartmentEdit].resetFields();
            } 
              this.$message.success(res.data.msg);
          } else {
            this.$message.error(res.data.msg);
          }
        })
      })
      }
    }
}
</script>
